<template>
  <div class="adv">
    <el-tabs v-model="activeName" type="card" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane v-for="v in adv_location" :key="v.value" :label="v.label" :name="v.value">
      </el-tab-pane>
      <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="广告名称" prop="adsName">
            <el-input v-model="queryParams.adsName" placeholder="请输入广告名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button type="success" v-if="activeName!='0'" plain icon="Plus" @click="handleAdd" v-hasPermi="['decor:ads:add']">新增</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="adsList">
          <el-table-column label="广告ID" align="center" prop="adsId" />
          <el-table-column label="广告名称" align="center" prop="adsName"></el-table-column>
          <el-table-column label="广告位置" align="center" prop="advLocation">
            <template #default="scope">
              <dict-tag :options="adv_location" :value="scope.row.advLocation" />
            </template>
          </el-table-column>
          <el-table-column label="广告状态" :show-overflow-tooltip="true" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" :show-overflow-tooltip="true" align="center" prop="createTime"></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['decor:ads:edit']">修改</el-button>
              <el-button link type="warning" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['decor:ads:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList(activeName)" />
      </div>
    </el-tabs>
  </div>
</template>
<script setup>
const { proxy } = getCurrentInstance();
let activeName = sessionStorage.getItem('currentActiveName') || '0'
import { listAds, delAds } from "@/api/decor/advertisement";
const router = useRouter();
const { adv_location } = proxy.useDict('adv_location')
const { status } = proxy.useDict('status')
// 表格数据
const adsList = ref([]);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    adsName: null,
    orderNum: null,
  },
});
const { queryParams } = toRefs(data);

/** 查询广告列表 */
function getList(advLocation) {
  loading.value = true;
  const data = {
    ...queryParams.value,
    advLocation: advLocation
  }
  listAds(data).then(response => {
    adsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
getList('0')
const handleClick = (tab) => {
  sessionStorage.setItem('currentActiveName',tab.props.name)
  // activeName = tab.props.name
  switch (tab.props.name) {
    case '0':
      getList('0')
      break;
    case '1':
      getList('1')
      break;
    case '2':
      getList('2')
      break;
    case '3':
      getList('3')
      break;
    default:
      getList('0')
      break;
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList(activeName);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  router.push('/advertisement/editAdv/' + activeName)
}
/** 修改按钮操作 */
function handleUpdate(row) {
  router.push('/advertisement/editAdv/' + activeName + '/' + row.adsId)
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _adsIds = row.adsId;
  proxy.$modal.confirm('是否确认删除首页轮播中广告名称为"' + row.adsName + '"的数据？').then(function () {
    return delAds(_adsIds);
  }).then(() => {
    getList(activeName);
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}
</script>
<style lang="scss" scoped>
.adv {
  margin: 20px;
}
</style>